package com.immediatus.graphics.texture.region;


import com.immediatus.graphics.contracts.ITexture;

public class TabledTextureRegion extends BaseTextureRegion{

    private final int _columns;
    private final int _rows;
    private int _currentColumn;
    private int _currentRow;
    private final int _cellCount;

    public TabledTextureRegion(final ITexture texture_, final int x_, final int y_, final int w_, final int h_, final int columns_, final int rows_){
        super(texture_, x_, y_, w_, h_);
        this._columns = columns_;
        this._rows = rows_;
        this._cellCount = this._columns * this._rows;
        this._currentColumn = 0;
        this._currentRow = 0;

        this.initTextureBuffer();
    }

    @Override
    protected void initTextureBuffer(){
        if (this._rows != 0 && this._columns != 0){
            super.initTextureBuffer();
        }
    }

    public int getCellCount(){
        return this._cellCount;
    }

    public int getCellWidth(){
        return super.getWidth() / this._columns;
    }

    public int getCellHeight(){
        return super.getHeight() / this._rows;
    }

    public int getCurrentColumn(){
        return this._currentColumn;
    }

    public int getCurrentRow(){
        return this._currentRow;
    }

    public int getCurrentCellIndex(){
        return this._currentRow * this._columns + this._currentColumn;
    }

    public void setCurrentCellIndex(final int column_, final int row_){
        if (column_ != this._currentColumn || row_ != this._currentRow){
            this._currentColumn = column_;
            this._currentRow = row_;
            super.updateTextureRegionBuffer();
        }
    }

    public void setCurrentCellIndex(final int index_){
        if (index_ < this._cellCount){
            final int columns = this._columns;
            this.setCurrentCellIndex(index_ % columns, index_ / columns);
        }
    }

    public int getTexturePositionOfCurrentCellX(){
        return super.getX() + this._currentColumn * this.getCellWidth();
    }

    public int getTexturePositionOfCurrentCellY(){
        return super.getY() + this._currentRow * this.getCellHeight();
    }

    @Override
    public TabledTextureRegion deepCopy(){
        final TabledTextureRegion deepCopy = new TabledTextureRegion(this.getTexture(), this.getX(), this.getY(), this.getWidth(), this.getHeight(), this._columns, this._rows);
        deepCopy.setCurrentCellIndex(this._currentColumn, this._currentRow);
        return deepCopy;
    }

    @Override
    public float getTextureCoordinateX1(){
        return (float) this.getTexturePositionOfCurrentCellX() / this.getTexture().getWidth();
    }

    @Override
    public float getTextureCoordinateY1(){
        return (float) this.getTexturePositionOfCurrentCellY() / this.getTexture().getHeight();
    }

    @Override
    public float getTextureCoordinateX2(){
        return (float) (this.getTexturePositionOfCurrentCellX() + this.getCellWidth()) / this.getTexture().getWidth();
    }

    @Override
    public float getTextureCoordinateY2(){
        return (float) (this.getTexturePositionOfCurrentCellY() + this.getCellHeight()) / this.getTexture().getHeight();
    }

    public void nextCell(){
        final int index = (this.getCurrentCellIndex() + 1) % this.getCellCount();
        this.setCurrentCellIndex(index);
    }

    public void prevCell(){
        final int index = (this.getCurrentCellIndex() - 1);
        this.setCurrentCellIndex(index < 0 ? this.getCellCount() - 1 : index);
    }
}
